package com.orangelife.service.propFeeBillLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeBillLog.IPropFeeBillLogService;
import com.orangelife.service.propFeeBillLog.model.PropFeeBillLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeBillLog类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-12-28 11:53:16
 * ==============================================================
 */
@Service("propFeeBillLogService")
public class PropFeeBillLogServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeBillLogService {
	private static Logger logger = Logger
			.getLogger(PropFeeBillLogServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeBillLogServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeBillLog(PropFeeBillLogDto propFeeBillLog)
			throws AppBizException {

		Assert.notNull(propFeeBillLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

	

		if (!StringUtils.isBlank(propFeeBillLog.getOperation())) {
			if (propFeeBillLog.getOperation().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作  1:新增，2：修改，3：删除 长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBillLog.getType())) {
			if (propFeeBillLog.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作类型 1预存明细  2 其他费用长度超出");
			}
		}

	

		propFeeBillLog.setLogID(IdUtil.getuuId());
		propFeeBillLog.setItemStat(TypeConst.FLAG_ON);
		propFeeBillLog.setTscreate(new Date());
		propFeeBillLog.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeBillLogMapper.insertSelective", propFeeBillLog);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeBillLog(PropFeeBillLogDto propFeeBillLog)
			throws AppBizException {

		Assert.notNull(propFeeBillLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeBillLog.getLogID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeBillLogDto oldPropFeeBillLog = this
				.findPropFeeBillLogByID(propFeeBillLog.getLogID());

		if (!StringUtils.isBlank(propFeeBillLog.getOperation())) {
			if (propFeeBillLog.getOperation().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作  1:新增，2：修改，3：删除 长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBillLog.getType())) {
			if (propFeeBillLog.getType().length() > 10) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"操作类型 1预存明细  2 其他费用长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeBillLog.getParameter())) {
			if (propFeeBillLog.getParameter().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "参数长度超出");
			}
		}

		if (oldPropFeeBillLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeBillLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeBillLogMapper.updateByPrimaryKeySelective",
				propFeeBillLog);
	}

	@Override
	public PropFeeBillLogDto findPropFeeBillLogByID(String propFeeBillLogID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeBillLogID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeBillLogDto oldPropFeeBillLog = this.getSqlSession().selectOne(
				"PropFeeBillLogSearchMapper.selectByPrimaryKey",
				propFeeBillLogID);
		if (oldPropFeeBillLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeBillLog.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeBillLog;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeBillLogByLogic(String propFeeBillLogID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeBillLogID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeBillLogDto oldPropFeeBillLog = this
				.findPropFeeBillLogByID(propFeeBillLogID);

		oldPropFeeBillLog.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeBillLog.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeBillLogMapper.updateByPrimaryKeySelective",
				oldPropFeeBillLog);
	}

	@Override
	public List<Map<String, Object>> findPropFeeBillLogByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeBillLogSearchMapper.findAllPropFeeBillLog", map);
		return list;
	}

	@Override
	public List<PropFeeBillLogDto> getPropFeeBillLogByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeBillLogDto> list = this.getSqlSession().selectList(
				"PropFeeBillLogSearchMapper.getAllPropFeeBillLogDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeBillLogByRelevantID(
			String logID, String type) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("logID", logID);
		map.put("type", type);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeBillLogSearchMapper.selectPropFeeBillLogDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeBillLogDto> findPropFeeBillLogByRelevantID(String logID,
			String type) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("logID", logID);
		map.put("type", type);

		List<PropFeeBillLogDto> list = this.getSqlSession().selectList(
				"PropFeeBillLogSearchMapper.findPropFeeBillLogDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeeBillLogDto> bauthfindPropFeeBillLog(
			List<String> propFeeBillLogIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeBillLogDto> list = this.getSqlSession().selectList(
				"PropFeeBillLogSearchMapper.batchFindPropFeeBillLog",
				propFeeBillLogIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeBillLogByMap(
			Map<String, Object> propFeeBillLog) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeBillLogSearchMapper.batchFindPropFeeBillLogByMap",
				propFeeBillLog);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}